//
//  AppDelegate.m
//  LoveFruit
//
//  Created by 张令林 on 16/9/22.
//  Copyright © 2016年 张令林. All rights reserved.
//

#import "AppDelegate.h"
#import "AppDelegate+Base.h"
#import "LFLoginViewController.h"
#import "ZLIntroductionViewController.h"
#import "LFNavigationController.h"
#import "LFTabBarController.h"
#import "LFLoginViewController.h"
#import "LFLoadSiteController.h"

#import <SMS_SDK/SMSSDK.h>

#define Appkey @"1762cf5e5aee8"

#define AppSecret @"9ea691fdc8c3c2f2c61cf121e5cd1482"


@interface AppDelegate ()
@property (nonatomic, strong) ZLIntroductionViewController *introductionViewVC;

@property (nonatomic, strong) NSArray *coverImageNames;

@property (nonatomic, strong) NSArray *backgroundImageNames;

@property (nonatomic, strong) NSArray *coverTitles;

@property (nonatomic, strong) NSURL *videoURL;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {//引导页1
    //注册短信
    [SMSSDK registerApp:Appkey
             withSecret:AppSecret];
    
    //设置window
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    UIViewController *vc = [UIViewController new];
    self.window.rootViewController = vc;
    [self.window makeKeyAndVisible ];
    
    //调用方法判断是否是首次安装应用
    if ([self isFirstInstall])
    {
        //是首次安装
        [self loadNewFeatureViewVideo];
        
        
    }else
    {
        LFTabBarController *vc = [LFTabBarController new];
        self.window.rootViewController = vc;
    }
    
    return YES;
}

#pragma mark - 首次安装调用的方法
//加载新特性页面(背景为视频)
- (void)loadNewFeatureViewVideo
{
    //添加视图
    self.coverImageNames = @[@"page01", @"page02", @"page03",@"page04"];
    self.backgroundImageNames = @[@"img_index_01bg", @"img_index_02bg", @"img_index_03bg",@"img_index_03bg"];
    self.coverTitles = @[@"MAKE THE WORLD", @"THE BETTER PLACE"];
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"loveFruit" ofType:@"mp4"];
    self.videoURL = [NSURL fileURLWithPath:filePath];
    
    
    self.introductionViewVC = [[ZLIntroductionViewController alloc] initWithVideo:self.videoURL volume:0.7];
    self.introductionViewVC.coverImageNames = self.coverImageNames;
    self.introductionViewVC.autoScrolling = YES;
    [self.window addSubview:self.introductionViewVC.view];
    
    
    __weak AppDelegate *weakSelf = self;
    self.introductionViewVC.didSelectedEnter = ^() {
        weakSelf.introductionViewVC = nil;
        //切换根控制器
        LFTabBarController *tabBarVC = [[LFTabBarController alloc] init];
        weakSelf.window.rootViewController = tabBarVC;
        [weakSelf.window makeKeyAndVisible];
        //显示 我知道了页面
        [weakSelf notify];
    };
    
    
}
//加载新特性页面(背景为图片)
- (void)loadNewFeatureView
{
    NSArray *coverImageNames = @[@"page01", @"page02", @"page03",@"page04"];
    NSArray *backgroundImageNames = @[@"img_index_01bg", @"img_index_02bg", @"img_index_03bg",@"img_index_03bg"];
    self.introductionViewVC = [[ZLIntroductionViewController alloc] initWithCoverImageNames:coverImageNames backgroundImageNames:backgroundImageNames];
    
    [self.window addSubview:self.introductionViewVC.view];
    __weak AppDelegate *weakSelf = self;
    self.introductionViewVC.didSelectedEnter = ^() {
        [weakSelf.introductionViewVC.view removeFromSuperview];
        weakSelf.introductionViewVC = nil;
        //跳转到登录页面
    };
    
    
    self.window.rootViewController = self.introductionViewVC;
    //显示 我知道了页面
    [self notify];
}
//显示 我知道了页面
-(void)notify
{
    UIView *guideView = [[UIView alloc] initWithFrame:self.window.bounds];
    guideView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    [self.window addSubview:guideView];
    UIImageView *topImageView = [[UIImageView alloc] init];
    topImageView.image = [UIImage imageNamed:@"homepage_guide"];
    [guideView addSubview:topImageView];
    [topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(guideView).offset(-6);
        make.top.equalTo(guideView).offset(26);
    }];
    UIImageView *bottomImageView = [[UIImageView alloc] init];
    bottomImageView.image = [UIImage imageNamed:@"homepage_knownbtn"];
    [guideView addSubview:bottomImageView];
    [bottomImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(guideView);
        make.top.equalTo(topImageView.mas_bottom).offset(100);
    }];
    // 点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [guideView addGestureRecognizer:tap];
    
}
//实现点击手势
-(void)tapAction:(UITapGestureRecognizer *)recognizer
{
        UIView *guide = recognizer.view;
        [UIView animateWithDuration:0.5 animations:^{
            guide.alpha = 0;
        } completion:^(BOOL finished) {
            [guide removeFromSuperview];
            
            //获取第一次定位到的位置
            
            LFLoadSiteController *vc = [[LFLoadSiteController alloc] init];
            [self.window.rootViewController addChildViewController:vc];
            __weak LFLoadSiteController *weakVC = vc;
            vc.showCity = ^{
                if (weakVC.isFirstTime == YES)
                {
                    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"设置当前城市?" message:@"确定" preferredStyle:UIAlertControllerStyleAlert];
                    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                        NSLog(@"点击了取消");
                    }];
                    UIAlertAction *certain = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                        NSLog(@"点击了确认");
                        //保存第一次获取到的位置
                        [[NSUserDefaults standardUserDefaults] setObject:weakVC.loadcity forKey:@"firstLoadCity"];
                        [[NSNotificationCenter defaultCenter] postNotificationName:@"isFirstTimeSelectCity" object:nil];
                        
                    }];
                    [alertVC addAction:cancel];
                    [alertVC addAction:certain];
                    //显示alert
                    [self.window.rootViewController presentViewController:alertVC animated:YES completion:nil];
                    weakVC.isFirstTime = NO;
                    
                    
                }
                
            };
        }];
}




- (void)applicationWillResignActive:(UIApplication *)application {

}

- (void)applicationDidEnterBackground:(UIApplication *)application {

}

- (void)applicationWillEnterForeground:(UIApplication *)application {

}

- (void)applicationDidBecomeActive:(UIApplication *)application {

}

- (void)applicationWillTerminate:(UIApplication *)application {

}


@end
